package com.didispace.web;

import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/ribbon-consumer-user", method = RequestMethod.GET)
    public String UserConsumer() {

        StringBuilder result = new StringBuilder();

        String titleOfGet1 = "GET 请求 第一种：getForEntity 函数。";
        result.append(titleOfGet1).append("<br>");
        // GET 请求  第一种：getForEntity 函数。 p75
        // getForEntity(String url, Class<T> responseType, Object... urlVariables)
        // 而返回的 ResponseEntity对象中的 body 内容类型会根据第二个参数转换为 String 类型
        // RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://USER-SERVICE/user1?name={1}", String.class, "didi");
        String body = responseEntity.getBody();
        result.append(body).append("<br>");

        // 返回的 body 是一个 User 对象类型
        // This application has no explicit mapping for /error, so you are seeing this as a fallback.
        // RestTemplate restTemplate = new RestTemplate();
        // ResponseEntity<User> responseEntity3 = restTemplate.getForEntity("http://USER-SERVICE/user3/{1}", User.class, "didi");
        //
        // User body3 = responseEntity3.getBody();
        // result.append(body3.getName()).append("<br>");

        // p76
        // getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)
        // RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        params.put("name", "dada");
        ResponseEntity<String> responseEntity4 = restTemplate.getForEntity("http://USER-SERVICE/user1?name={name}", String.class, params);
        String body4 = responseEntity4.getBody();
        result.append(body4).append("<br>");

        // p76
        // getForEntity(URI url, Class<T> responseType)
        // RestTemplate restTemplate = new RestTemplate();
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(
                "http://USER-SERVICE/user1?name={name}")
                .build()
                .expand("dodo")
                .encode();
        URI uri = uriComponents.toUri();
        // ERROR：ResponseEntity<String> responseEntity5 = restTemplate.getForEntity(uri, String.class).getBody();
        String body5 = restTemplate.getForEntity(uri, String.class).getBody();
        result.append(body5).append("<br>");

        String titleOfGet2 = "GET 请求 第二种：getForObject 函数。";
        result.append(titleOfGet2).append("<br>");
        // GET 请求  第二种：getForObject 函数。 p77
        // RestTemplate restTemplate = new RestTemplate();
        String result1 = restTemplate.getForObject(uri, String.class);
        result.append(result1).append("<br>");

        // RestTemplate restTemplate = new RestTemplate();
        // 2023-01-12 23:49:03.547  WARN 12128 --- [nio-9000-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'dodo': was expecting ('true', 'false' or 'null')
        // at [Source: java.io.PushbackInputStream@4cc38078; line: 1, column: 9]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'dodo': was expecting ('true', 'false' or 'null')
        // at [Source: java.io.PushbackInputStream@4cc38078; line: 1, column: 9]
        // User result2 = restTemplate.getForObject(uri, User.class);
        // String userName = JSONObject.toJSONString(result2);
        // result.append(userName).append("<br>");

        String titleOfPost1 = "POST 请求 第一种：postForEntity 函数。";
        result.append(titleOfPost1).append("<br>");
        // POST 请求 第一种：postForEntity 函数。 p77
        // RestTemplate restTemplate = new RestTemplate();
        User user = new User("didi", 30);
        ResponseEntity<String> responseEntity6 =
                restTemplate.postForEntity("http://USER-SERVICE/user3", user, String.class);
        String body6 = responseEntity6.getBody();
        result.append(body6).append("<br>");

        String titleOfPost2 = "POST 请求 第二种：postForObject 函数。";
        result.append(titleOfPost2).append("<br>");
        // POST 请求 第二种：postForObject 函数。 p78
        // RestTemplate restTemplate = new RestTemplate();
        User user2 = new User("didi", 20);
        String postResult = restTemplate.postForObject("http://USER-SERVICE/user3", user2, String.class);
        result.append(postResult).append("<br>");

        String titleOfPost3 = "POST 请求 第三种：postForLocation 函数。";
        result.append(titleOfPost3).append("<br>");
        // POST 请求 第三种：postForLocation 函数。 p78
        User user3 = new User("didi", 40);
        URI responseURI = restTemplate.postForLocation("http://USER-SERVICE/user3", user3);
        result.append(responseURI).append("<br>");

        String titleOfPut = "PUT 请求。";
        result.append(titleOfPut).append("<br>");
        // RestTemplate restTemplate = new RestTemplate();
        Long id = 10001L;
        User user4 = new User("didi", 40);
        restTemplate.put("http://USER-SERVICE/user4/{1}", user4, id);

        String titleOfDelete = "DELETE 请求。";
        result.append(titleOfDelete).append("<br>");
        // RestTemplate restTemplate = new RestTemplate();
        Long id2 = 10001L;
        restTemplate.delete("http://USER-SERVICE/user5/{1}", id2);

        return result.toString();
    }
}

// http://localhost:9000/ribbon-consumer-user
